#ifndef __OS_LIB_H
#define __OS_LIB_H

#include <stdint.h>

/* 计算结构体元素的偏移地址*/
#define offsetof(TYPE, MEMBER)         ((uint32_t) &((TYPE *)0)->MEMBER)

/** container_of : 由元素找到父结构体指针
 * ptr : 成员节点地址
 * type : 父结构数据类型
 * member: 成员节点名*/
#define container_of(ptr, type, member)  ({ \
     (type *)((uint32_t)ptr - offsetof(type,member));   \
    })

    
/**
 * @brief BITMAP_T位图类型，支持32位。
 */
typedef struct 
{
    uint32_t bitmap;        /*32位的位图*/
} BITMAP_T;

extern void         vBitmapInit(BITMAP_T *bitmap);
extern void         vBitmapSet(BITMAP_T *bitmap, uint32_t pos);
extern void         vBitmapClear(BITMAP_T *bitmap, uint32_t pos);
extern uint32_t     vBitmapGetFirstSet(BITMAP_T *bitmap);
extern uint32_t     vBitmapGetPosCount(void);



/**
 * @brief NODE_T链表节点类型
 */
typedef struct _NODE {
    struct _NODE *nextNode;     /* 当前节点的下一个节点*/
    struct _NODE *preNode;      /* 当前节点的上一个节点*/
} NODE_T;

/**
 * @brief NODE_T链表类型
 */
typedef struct _LIST {
    NODE_T headNode;            /* 链表的头节点 */
    uint32_t nodeCount;         /* 链表的节点个数*/
} LIST_T;

extern void        vNodeInit(NODE_T *node);

extern void        vListInit(LIST_T *list);
extern uint32_t    vListCount(LIST_T *list);
extern NODE_T*     vListFirst(LIST_T *list);
extern NODE_T*     vListLast(LIST_T *list);
extern NODE_T*     vListPre(LIST_T *list, NODE_T *node);
extern NODE_T*     vListNext(LIST_T *list, NODE_T *node);
extern void        vListRemoveAll(LIST_T *list);
extern void        vListAddFirst(LIST_T *list, NODE_T *node);
extern void        vListAddLast(LIST_T *list, NODE_T *node);
extern NODE_T*     vListRemoveFirst(LIST_T *list);
extern NODE_T*     vListRemoveLast(LIST_T *list);
extern void        vListInsterAfter(LIST_T *list, NODE_T *node, NODE_T *nodeToInsert);
extern void        vListRemove(LIST_T *list, NODE_T *node);


#endif /* __OS_LIB_H*/
